package com.lbd.dynamicCode;

import com.lbd.dynamicCode.bean.MsgSendRecord;
import com.lbd.dynamicCode.service.DynamicCodeService;
import com.lbd.dynamicCode.service.RestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;

/**
 * Demo class
 *
 * @author ${user}
 * @date ${date}
 */
@Component
public class AppRunner {
    // implements CommandLineRunner{
    private static final Logger logger = LoggerFactory.getLogger(AppRunner.class);

    @Resource
    private RestService gitHubLookupService;
    @Resource
    private DynamicCodeService dynamicCodeService;

    //@Override
    public void run(String... strings) throws Exception {
        // Start the clock
        long start = System.currentTimeMillis();

        // Kick of multiple, asynchronous lookups

        /*CompletableFuture<MsgUser> page1 = gitHubLookupService.findUser("PivotalSoftware");
        CompletableFuture<MsgUser> page2 = gitHubLookupService.findUser("CloudFoundry");
        CompletableFuture<MsgUser> page3 = gitHubLookupService.findUser("Spring-Projects");*/

        MsgSendRecord user=dynamicCodeService.findById("8a8002fd60167ec50160167f69d20000");
        //CompletableFuture<List<LinkResult>> getAndUpdate = gitHubLookupService.getShortUrl(user);
        CompletableFuture<Integer> shortUrl = gitHubLookupService.getShortUrl(user);
        // Wait until they are all done
        //CompletableFuture.allOf(page1,page2,page3).join();

        // Print results, including elapsed time
        logger.info("Elapsed time: " + (System.currentTimeMillis() - start));
        /*logger.info("--> " + page1.get());
        logger.info("--> " + page2.get());
        logger.info("--> " + page3.get());*/
        if(shortUrl.get()>0){
            System.out.println("SUCCESS");
        }else{
            System.out.println("FAIL");
        }
        System.in.read();

    }
}
